<?php

namespace App\Admin\Extensions\Grid;

use Encore\Admin\Admin;
use Encore\Admin\Grid\Displayers\AbstractDisplayer;
use Illuminate\Support\Facades\Storage;
use Illuminate\Contracts\Support\Arrayable;

class FancyImage extends AbstractDisplayer
{
    public function display($server = '', $width = 200, $height = 200)
    {

        Admin::css("/vendor/laravel-admin/FancyBox/jquery.fancybox.min.css");
        Admin::js("/vendor/laravel-admin/FancyBox/jquery.fancybox.min.js");

        return collect((array)$this->value)->filter()->map(function ($path) use ($server, $width, $height) {
            if (url()->isValidUrl($path)) {
                $src = $path;
            } else {
                $src = Storage::disk(config('admin.upload.disk'))->url($path);
            }
            return "<a title='查看大图' data-fancybox='gallery' href='$src'><img src='$src' style='max-width:{$width}px;max-height:{$height}px'></a>";

        })->implode('&nbsp;');

    }
}